from DrissionPage import ChromiumPage, ChromiumOptions
import time
import random

# 提供的变量
cookies = ['abRequestId=ea1b36e3-290b-5589-800d-ef6e4b7fed88; xsecappid=xhs-pc-web; a1=196ab51cec7t1ks552aduq8k2e1y9ed7ewbub2fi130000329117; webId=2ffc3ee8a1dd24d6d32f8d7a248d2307; acw_tc=0a0bb4b917466309878964371e60dd118106e99a359d579c40416d2dd21f0c; gid=yjK0D2yfSjd8yjK0D2ySdEJiSW9yTM22J0f7D3yAYTJdy7q8vjdfWD888qJjyyW8jWjSD8jK; webBuild=4.62.3; websectiga=2845367ec3848418062e761c09db7caf0e8b79d132ccdd1a4f8e64a11d0cac0d; sec_poison_id=e581ef89-e70f-44aa-988e-b4400f5a96ad; loadts=1746631540844; unread={%22ub%22:%2267f8726c000000001c0158ec%22%2C%22ue%22:%2267f993cb000000001d01c3b4%22%2C%22uc%22:29}; web_session=040069b45d0634f8cf12fb332e3a4bce08a203']
def get_random_user_agent():
    """生成随机User-Agent"""
    user_agents = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
    ]
    return random.choice(user_agents)

def login_to_chaoxing():
    """使用DrissionPage自动化登录学习通平台"""
    page = None
    try:
        # 创建ChromiumOptions对象并配置指纹
        co = ChromiumOptions()
        
        # 设置随机User-Agent
        co.set_user_agent(get_random_user_agent())
        
        # 设置浏览器指纹
        co.set_argument('--disable-blink-features=AutomationControlled')  # 禁用自动化控制特征
        co.set_argument('--disable-infobars')  # 禁用信息栏
        co.set_argument('--disable-notifications')  # 禁用通知
        co.set_argument('--disable-popup-blocking')  # 禁用弹窗拦截
        co.set_argument('--disable-save-password-bubble')  # 禁用保存密码提示
        co.set_argument('--disable-translate')  # 禁用翻译
        co.set_argument('--disable-web-security')  # 禁用网页安全策略
        co.set_argument('--no-sandbox')  # 禁用沙箱
        co.set_argument('--disable-gpu')  # 禁用GPU加速
        co.set_argument('--disable-dev-shm-usage')  # 禁用/dev/shm使用
        co.set_argument('--disable-extensions')  # 禁用扩展
        co.set_argument('--disable-plugins')  # 禁用插件
        co.set_argument('--disable-software-rasterizer')  # 禁用软件光栅化
        co.set_argument('--disable-features=IsolateOrigins,site-per-process')  # 禁用站点隔离
        co.set_argument('--disable-site-isolation-trials')  # 禁用站点隔离试验
        co.set_argument('--disable-web-security')  # 禁用网页安全策略
        co.set_argument('--disable-features=BlockInsecurePrivateNetworkRequests')  # 禁用不安全私有网络请求拦截
        co.set_argument('--disable-features=IsolateOrigins')  # 禁用源隔离
        co.set_argument('--disable-features=site-per-process')  # 禁用站点进程隔离
        
        # 创建ChromiumPage实例
        page = ChromiumPage(co)
        
        # 设置窗口大小
        page.set.window.size(1200, 1000)
        
        # 注入反检测JS
        page.run_js("""
            Object.defineProperty(navigator, 'webdriver', {
                get: () => undefined
            });
            Object.defineProperty(navigator, 'plugins', {
                get: () => [1, 2, 3, 4, 5]
            });
            Object.defineProperty(navigator, 'languages', {
                get: () => ['zh-CN', 'zh', 'en']
            });
        """)
        
        # 先访问网站
        page.get('https://www.xiaohongshu.com')
        time.sleep(random.uniform(2, 4))  # 随机等待时间
        
        # 设置cookies
        page.set.cookies(cookies)
        print("已设置学习通cookie")
        
        # 刷新页面以应用Cookie
        page.refresh()
        time.sleep(random.uniform(3, 5))  # 随机等待时间

        print("成功导航到学习通并应用Cookie！")

        # 等待用户按回车键关闭浏览器
        input("请按回车键关闭浏览器窗口...")

    except Exception as e:
        print(f"发生错误：{str(e)}")
    finally:
        # 关闭浏览器
        if page:
            try:
                page.quit()
                print("浏览器已关闭。")
            except:
                pass

if __name__ == "__main__":
    login_to_chaoxing()